package jpm.common;

import static jpm.common.AssertUtils.ensure;
import static jpm.common.AssertUtils.ensureEqual;
import static jpm.common.AssertUtils.ensureNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import jpm.common.JPMException.ErrorCode;

import org.junit.Test;

public class AssertUtilsTest {

	@Test
	public void ensure_true_doesNotThrow() {
		ensure(true); //
		ensure(true, "something wrong");
	}

	@Test
	public void ensure_false_throwsAssertFail() {
		try {
			ensure(false);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("false condition throws assert fail",
					ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
		}

		try {
			ensure(true == false, "error message");
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("false condition throws assert fail",
					ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
			assertTrue("error message captured",
					ex.getUserFriendlyMessage().contains("error message"));
		}
	}

	@Test
	public void ensure_nullMessage_throwsInvalidInput() {
		try {
			ensure(false, null);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("null messageIfError throws invalid input",
					ErrorCode.ERROR_INVALID_INPUT, ex.getErrorCode());
		}
	}

	@Test
	public void ensureNotNull_nonNull_doesNotThrow() {
		ensureNotNull("JPM");
		ensureNotNull(new Integer(5));
	}

	@Test
	public void ensureNotNull_null_throws() {
		try {
			ensureNotNull(null);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("assert fail for null object",
					ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
		}

		try {
			ensureNotNull(null, "error message");
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("assert fail for null object",
					ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
			assertTrue("specified error message captured", ex.getUserFriendlyMessage()
					.contains("error message"));
		}
	}

	@Test
	public void ensureNotNull_nullMessage_throwsInvalidInput() {
		try {
			ensureNotNull(null, null);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("null messageIfError throws invalid input",
					ErrorCode.ERROR_INVALID_INPUT, ex.getErrorCode());
		}
	}

	@Test
	public void ensureEqual_equal_doesNotThrow() {
		ensureEqual(5, 5);
		ensureEqual(5, 5, "something wrong");
	}

	@Test
	public void ensureEqual_unequal_throwsAssertFail() {
		try {
			ensureEqual(5, 6);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals(ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
		}

		try {
			ensureEqual(5, 6, "error message");
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals(ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
			assertTrue(ex.getUserFriendlyMessage().contains("error message"));
		}
	}

	@Test
	public void ensureEqual_nullMessage_throwsInvalidInput() {
		try {
			ensureEqual(5, 6, null);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("null messageIfError throws invalid input",
					ErrorCode.ERROR_INVALID_INPUT, ex.getErrorCode());
		}
	}
}
